/* 
 * Copyright [2018] [Alex/libo(liboms@hotmail.com)]
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.hyts.scanner.core;

import java.util.List;
import java.util.Objects;
import java.util.stream.Stream;

import com.hyts.scanner.base.AbstractScanner;
import com.hyts.scanner.bean.ScanBaseObject;
import com.hyts.scanner.bean.ScanExtObject;
import com.hyts.scanner.bean.ScanResultObject;
import com.hyts.scanner.helper.ClassHelper;
import com.hyts.scanner.helper.ScanHelper;


/** 
 * @title SourceScanner.java  
 * @package com.hyts.apidoc.scanner  
 * @description source代码相关的扫描机制，专属扫描source源码内,匹配此包结构的相关类,
 * 				默认扫描所有相关的机械性扫描类，可以控制扫描范围，扫描机制
 * 				递归，且可以根据系统提供相关的注解、继承类进行深层限制和控
 * 				制范围    
 * @author libo/Alex
 * @email liboms@hotmail.com/liboware@163.com  
 * @date 2018年4月18日 
 * @version V1.0 
 */
public class SourceAndJarHandler extends AbstractScanner{
	
	/**  
	 * <p>是否递归</p> 
	 * @fieldName isRecursive
	 * @fieldType boolean
	 */ 
	private boolean isRecursive;

	/**  
	 * @constructor：SourceScanner  
	 * @param isRecursive  
	 */ 
	public SourceAndJarHandler(boolean isRecursive) {
		super();
		this.isRecursive = isRecursive;
	}

	/**  
	 * @constructor：SourceScanner    
	 */ 
	public SourceAndJarHandler() {
		super();
		this.isRecursive = true;
	}
	
	/* (非 Javadoc)
	 * @param scanObject
	 * @return
	 * @see com.hyts.apidoc.base.AbstractScanner#scan(com.hyts.apidoc.bean.ScanBaseObject)  
	 * @exception
	 */ 
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public ScanResultObject scan(ScanBaseObject scanObject) {
		//判断属于那种解析操作，系统自动识别-进行切换扫描复杂度
		ScanResultObject result = new ScanResultObject();
				if(scanObject instanceof ScanBaseObject) {
					List<String> resultFileList= isRecursive?
								new ScanHelper().scanRecursive(scanObject):new ScanHelper()
							.scanNonRecursive(scanObject);
				for(String filePath:resultFileList)	{
					if(Objects.nonNull(scanObject.annotations) && scanObject.annotations.length != 0) {
						Stream.of(scanObject.annotations).
								forEachOrdered((annotation)->{
							Class clazz = ClassHelper.filePathToClass(filePath, scanObject.packages);
							if(clazz.isAnnotationPresent(annotation)) {
								result.addResultFile(ClassHelper.filePathToUserPath(filePath, scanObject.packages));
								result.addResultClass(ClassHelper.filePathToClass(filePath, scanObject.packages));
							}
						});
					}else {
						result.addResultFile(ClassHelper.filePathToUserPath(filePath, scanObject.packages));
						result.addResultClass(ClassHelper.filePathToClass(filePath, scanObject.packages));
					}
				}
			}else if(scanObject instanceof ScanExtObject){
					ScanExtObject newScanObject = (ScanExtObject)scanObject;
		}
		return result;
	}
}
